<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Adminb_PmController extends App_Zend_Controller_Action_Adminb
{
	

	public function init(){
		parent::init();
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			if($_COOKIE[COOKIE_PRE.'uid']!=''){
				
				$this->_redirect(MY_URL.'/buyer');
				
			}else{
			
				$this->_redirect(MY_URL.'/buyer-auth-login');
				
			}
			
		}
		
	}
	
	public function preDispatch()
	{

		$this->getHelper ('layout')->setLayout('framelayout');

		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		$pmfl=new CFenlei('pm');
		
		$flArray=$pmfl->getFenleiByPid(0);
		
		$this->view->assign('flArray',$flArray);
		
		$fromto=$this->_request->getParam('fromto','to');
		
		$this->view->assign('fromto',$fromto);
		
		$flid=$this->_request->getParam('flid',0);
    	
    	$this->view->assign('flid',$flid);
		
		
	}

	
    public function indexAction() 
    { 	

    	$flid=$this->_request->getParam('flid','0');
    	
    	$fromto=$this->_request->getParam('fromto','to');
    	
    	$page=$this->_request->getParam("page",1);
		
		$this->view->assign('flid',$flid);
		
		$uid=$_COOKIE[COOKIE_PRE.'uid'];
		
		$pm=new CPm();
		
		$pmCounts=$pm->getPmCounts($uid,$flid,$fromto);
		
    	Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $pmCounts, 5 );
		
		$pmArray=$pm->getPms($uid,$flid,$fromto,$pageArray['from'],5);
		
		Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::divPagesAdmin( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/adminb-pm-index?fromto=$fromto&flid=$flid"); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );

		$this->view->assign ( 'pmArray', $pmArray );

    }
    
    public function addAction() 
    { 	

    	$flid=$this->_request->getParam('flid','0');
    	
    	$mustFlid=$this->_request->getParam('mustFlid','4');
    	
    	$mustFlid=$mustFlid=='' ? 4 : $mustFlid;
    	
    	$sendto=$this->_request->getParam('sendto');
		
    	$this->view->assign('sendto',$sendto);
    	
		$this->view->assign('flid',$flid);
		
		$this->view->assign('mustFlid',$mustFlid);
		
		$orderid=$this->_request->getParam('orderid');
		
    	$this->view->assign('orderid',$orderid);
    	
    	
    	$subject=$this->_request->getParam('subject');
    	
    	
    	
    	$caseitemid=$this->_request->getParam('caseitemid');
		
    	$this->view->assign('caseitemid',$caseitemid);
    	
    	
    	$content='';
    	
    	if($orderid!=''){
    	
    		$content=$this->view->getOrderInfo($orderid);
    	
    	}else if($caseitemid!=''){
    		
    		$content=$this->view->getCaseInfo($caseitemid,$sendto,$subject);
    		
    	}
    	
    	$this->view->assign('subject',$subject);
    	
    	require_once (MY_ROOT."/fckeditor/fckeditor.php");
		
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		
		$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
		
		$oFCKeditor->Value		= $content;
		
		$oFCKeditor->Height=400;
		
		$oFCKeditor->Width="98%";
		
		$this->view->assign('oFCKeditor',$oFCKeditor);	

    }
    
    public function doaddAction(){  
    	
        if(!$this->getRequest()->isPost()){
			
			$this->view->msg = "error!";
			
			return false;
			
		}
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
    	
    	$member=new CMember();
    	
    	$to_uid=0;
    	
    	if($thisArray['idUsername']==1){
    		
    		$userArray=$member->getUserDetails(array('username'=>$thisArray['to_who']));
    		
    		if(count($userArray)!=1){
    			
    			$this->view->msg = "The user does not exist!";
			
				return false;
    			
    		}
    		
    		$to_uid=$userArray[0]['uid'];
    		
    	}else{
    		
    		if($member->getUserCounts(array('uid'=>$thisArray['to_who']))!=1){
    			
    			$this->view->msg = "The user does not exist!";
			
				return false;
    		
    		}
    		
    		$to_uid=$thisArray['to_who'];
    		
    	}
    	
    	$data=array(
    				'from_uid'=>$_COOKIE[COOKIE_PRE.'uid'],
    				'to_uid'=>$to_uid,
    				'add_time'=>time(),
    				'subject'=>$input->subject,
    				'content'=>htmlspecialchars_decode($input->FCKeditor1),
    				'has_read'=>0,
    				'flid'=>$thisArray['flid'],
    			   );
    			   
    	$pm=new CPm();
    	
    	$pm->createPm($data);
    	
    	$this->view->msg = "Sent successfully!";
    	
    	return true;
    	
    }
    
    public function delAction() 
    { 	
		$thisArray=$this->_request->getParams();
		
		$fromto=$this->_request->getParam('fromto','to');
		
		$pm=new CPm();
		
		if($thisArray['pmid']!=''){
			
			$pm->setDel(array($thisArray['pmid']),$fromto);
			
		}else if(is_array($thisArray['itemArray'])){

			$pm->setDel($thisArray['itemArray'],$fromto);
			
		}
    }
    
    public function moreAction() 
    { 	
    	
    	$pmid=$this->_request->getParam('pmid');
    	
    	$pm=new CPm();

		$pmArray=$pm->getDetail($pmid);
		
		if($pmArray[0]['to_uid']==$_COOKIE[COOKIE_PRE.'uid']){
			
			$pm->editPm($pmid,array('has_read'=>'1'));
			
		}
		
		$this->view->assign('pmArray',$pmArray);

    }
	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
